/* 
 **********************************************************************************
 * Copyright www.ebidding.net.cn 2014-12-15 Authors: 闫广坤 <ygk@ebidding.com.cn>*
 **********************************************************************************
 */
package com.example.demo.core.util;
 
import org.apache.commons.lang.StringUtils;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import java.io.InputStream;
import java.util.Properties;

/**
 * 作者：闫广坤
 * 创建时间：2014-12-15
 * 功能描述：
 * 版本:2.0.0
 */
public class AppConfig {
 
	private static PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
	 
	private static Properties ps;     
   
	public synchronized static void init() {
       
       try { 
    	   Resource[] resources = resolver.getResources("classpath*:config.properties");
    	   Resource resource = resources[0];
           InputStream input = resource.getInputStream();
           ps = new Properties();
           ps.load(input);
       } catch (Exception ex) {
    	   ex.printStackTrace();
    	   ps = null;
       }
	}    
   
	public static String getString(String key) {
       
		return getValue(key);
	}
   
	public static String getProperty(String key) {
       
		return getString(key);
	}
   
	public static int  getInt(String key) {
       
		String value = getValue(key);
		if(StringUtils.isNotEmpty(value))
			return new Integer(value);
       
		return 0;
   }
   
	public static String  getValue(String key) {
       
		if(ps == null)
			init();
       
		try {
			String value = ps.getProperty(key);
			if(StringUtils.isEmpty(value)) return "";
           
			value = new String(value.getBytes("iso-8859-1"),"utf-8");
			return StringUtils.replace(value,"\\", "/");
		} catch(Exception e) {
			e.printStackTrace();
		}
		return "";
   }
}
